// @vue/component
import {
	List,
	Delete,
	Save,
	Update
} from '../../api/user'
import {
	List as pList
}
from '../../api/permission.js'
import {
	mixin
} from "../../utils/mixins";
import Header from "../../components/Header/Header.vue"
import Dialog from "../../components/Dialog/Dialog.vue"
import {
	objFilter
} from "../../utils/objFilter"
export default {
	name: 'User',

	components: {
		Dialog,
		Header
	},

	mixins: [mixin],

	props: {},

	data() {
		return {
			dataForm: {
				key: ''
			},
			options: {
				list: ["用户名", "密码", "权限"],
				data: {
					name: '',
					password: '',
					permissionId: null
				},
				upload: true,
				type: "用户",
				rules: {
					name: [{
						required: true,
						message: '用户名不能为空',
						trigger: 'blur'
					}],
					password: [{
						required: true,
						message: '密码不能为空',
						trigger: 'blur'
					}]
				}

			},
			dataList: [],
			pageIndex: 1,
			pageSize: 10,
			totalPage: 0,
			totalCount: 0,
			dialogTableVisible: false,
			labelPosition: 'right',
			permissionList: [],
			rules: {
				name: [{
					required: true,
					message: '用户名不能为空',
					trigger: 'change'
				}],
				password: [{
					required: true,
					message: '密码不能为空',
					trigger: 'change'
				}]
			}
		}
	},

	computed: {},

	watch: {},

	created() {
		this.getUserList()
		this.getPermissionList()
		console.log("created==========")
	},

	methods: {
		showDialog(type, data, id) {
			if (type == "update") {
				data = objFilter({}, ["name", "password", "permissionId"], data, id)
				this.$refs.dialog.showDialog(type, data, id);
			} else {
				this.$refs.dialog.showDialog(type);
			}
		},
		getUserList: function() {
			const _this=this
			List({
				'page': this.pageIndex,
				'limit': this.pageSize,
				'key': this.dataForm.key
			}).then(response => {
				_this.dataList = response.list;
				_this.totalPage = response.totalPage;
				_this.totalCount = response.totalCount;
			})
		},
		saveUserList: function() {
			let params = this.options.data
			Save(params).then(res => {
				this.notify("添加成功", "success");
				this.getUserList()
			})
		},
		updateUserList: function(id) {
			let params = this.options.data
			params["id"] = id
			Update(params).then(res => {
				this.notify("修改成功", "success");
				this.getUserList()
			})

		},
		deleteById: function(id) {
			let ids = [];
			ids.push(id);
			console.log("===>" + ids)
			Delete(ids).then(response => {
				if (response.code === 200) {
					this.notify("删除成功", "success");
					this.getUserList();
				} else {
					this.notify("删除失败", "fail");
				}

			})
		},
		handleSizeChange(val) {
			this.pageSize = val
			this.getUserList();
		},
		handleCurrentChange(val) {
			this.pageIndex = val
			this.getUserList();
		},
		handleEdit(index, row) {
			console.log(index, row);
		},
		handleDelete(index, row) {
			this.deleteById(row.id);
		},
		search(e) {
			this.dataForm.key = e
			this.getUserList()
		},
		// 获取权限列表
		getPermissionList: function() {
			pList({
				'page': 1,
				'limit': 10,
			}).then(response => {
				console.log("permissionList-=======")
				console.log( response.page.list)
				this.$set(this.permissionList, 0,response.page.list);
			})
		},

	}
}
